package com.cskaoyan.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @BelongProject: mall35th
 * @BelongPackage: com.cskaoyan.utils
 * @Author: zijin
 * @Createtime: 2021-12-02 星期四 17:42
 * @Description: md5加密，用于user表和admin用户表数密码的加密
 */
public class Md5Util {
    //石头老师，牛批
    private static final String SALT = "shitoulaoshi niupi";

    public static String getMd5(String content) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] bytes = content.getBytes();
        byte[] digest = messageDigest.digest(bytes);
        //将byte转换成16进制值字符串的形式
        StringBuffer stringBuffer = new StringBuffer();
        for (byte b : digest) {
            int i = b & 0xff; //0-255 00-ff
            String s = Integer.toHexString(i);
            //0a
            if (s.length() == 1){
                stringBuffer.append(0);
            }
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }

    /**
     * 默认加盐md5加密
     * @param content
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getMd5AdDefaultSalt(String content) throws NoSuchAlgorithmException {
        return getMd5(content+SALT);
    }

    /**
     * 自定义加盐加密
     * @param content
     * @param salt
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getMd5AddSalt(String content,String salt) throws NoSuchAlgorithmException {
        return getMd5(content+salt);
    }
}
